import json
import pandas as pd

MOVIE_LEN_FILE_PATH = '../data/origin-data/'
TMDB_JSON_FILE_PATH = '../data/json/'

# 将数据存入到json文件中
def store_data_in_json(data, file_name):
  file_path = TMDB_JSON_FILE_PATH + file_name
  with open(file_path, 'w', encoding='utf-8') as f:
    json.dump(data, f)

# 将数据从csv中取出来
def get_data_from_csv(file_name):
  file_path = MOVIE_LEN_FILE_PATH + file_name
  df = pd.read_csv(file_path)
  columns = df.columns
  # 过滤掉所有列都是 NaN 的行
  df = df.dropna()
  result = []
  for row in df.itertuples(index=True):
    obj = {}
    for _, col_name in enumerate(columns):
      obj[col_name] = getattr(row, col_name)
    result.append(obj)
  return result

# 获取json文件的数据
def get_data_from_json(file_name):
  file_path = TMDB_JSON_FILE_PATH + file_name
  with open(file_path, 'r', encoding='utf-8') as f:
    return json.load(f)
